package com.c611.smartclassroom.service.Impl;

import com.c611.smartclassroom.mapper.ZoneMapper;
import com.c611.smartclassroom.model.Zone;
import com.c611.smartclassroom.model.ZoneExample;
import com.c611.smartclassroom.service.ZoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ZoneServiceImpl implements ZoneService {


    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    ZoneMapper zoneMapper;

    @Cacheable(value = "zone",key = "'queryZoneCount::' + #schoolID")
    @Override
    public long queryZoneCount(String schoolID){
        ZoneExample zoneExample = new ZoneExample();
        zoneExample.createCriteria().andDelflagNotEqualTo(1).andSchoolidEqualTo(schoolID);
        return zoneMapper.countByExample(zoneExample);
    }


    /**
     * 查询所有区域
     * @return
     */
    @Cacheable(value = "zone",key = "'school::' + #schoolID ",unless = "#result == null ")
    @Override
    public List<Zone> queryAllZone(String schoolID) {
        ZoneExample zoneExample = new ZoneExample();
        zoneExample.createCriteria().andDelflagNotEqualTo(1).andSchoolidEqualTo(schoolID);
        return zoneMapper.selectByExample(zoneExample);
    }

    /**
     * 设置微信的方法
     * @return
     */
    @CacheEvict(value = "zone",allEntries = true)
    @Override
    public int setWeChat(String zoneID, String weChat) {

        ZoneExample zoneExample = new ZoneExample();
        zoneExample.createCriteria().andZoneidEqualTo(zoneID).andDelflagEqualTo(0);

        Zone zone = new Zone();
        zone.setZoneid(zoneID);
        zone.setWechat(weChat);
        return zoneMapper.updateByExampleSelective(zone,zoneExample);
    }

    /**
     * 增加区域
     * @return int
     */
    @CacheEvict(value = "zone",allEntries = true)
    @Override
    public int addZone(Zone zone) {
        zone.setAddtime(new Date());
        zone.setDelflag(0);
        return zoneMapper.insert(zone);
    }

    /**
     * 编辑区域
     * @return
     */
    @CacheEvict(value = "zone",allEntries = true)
    @Override
    public int saveZone(Zone zone) {
        String zoneID = zone.getZoneid();
        ZoneExample zoneExample= new ZoneExample();
        zoneExample.createCriteria().andZoneidEqualTo(zoneID).andDelflagNotEqualTo(1);

        return zoneMapper.updateByExampleSelective(zone,zoneExample);
    }

    /**
     * 删除区域
     * @return
     */
    @CacheEvict(value = "zone",allEntries = true)
    @Override
    public int delZone(String zoneID) {
        ZoneExample zoneExample = new ZoneExample();
        zoneExample.createCriteria().andZoneidEqualTo(zoneID).andDelflagEqualTo(0);

        Zone zone = new Zone();
        zone.setZoneid(zoneID);
        zone.setDelflag(1);
        return zoneMapper.updateByExampleSelective(zone,zoneExample);
    }

    @Cacheable(value = "zone",key = "'ckeckZoneId::' + #zoneid ")
    @Override
    public int ckeckZoneId(String zoneid) {
        Zone zone = zoneMapper.selectByPrimaryKey(zoneid);
        if(zone != null) return 1;
        return 0;
    }
}
